﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Text;
using NHibernate;
using NHibernate.Criterion;
using OpenGado.DATA.Entities;

namespace OpenGado.DATA
{

    /// <summary>
    /// Class provides CRUD methods for the Project object
    /// </summary>
   public class ProjectDataControl
   {

       private static ProjectDataControl projectDataControl;
       private static object  lockProjectControl =new object();

        private ISession session;

       /// <summary>
       /// Return a valid session 
       /// </summary>
       public ISession Session
       {
           get
           {
               session = FactoryHandler.CreateFactory().OpenSession();
               return session;
           }
          
       }

       /// <summary>
       ///  Returns instance of project Data control object
       /// </summary>
       public static ProjectDataControl Instance
       {
           get 
           {
               lock(lockProjectControl)
                   if (projectDataControl == null)
                   {
                       projectDataControl = new ProjectDataControl();
                   }

               return projectDataControl;

           }
       }

       /// <summary>
       ///  Persists project object into the database.
       /// </summary>
       /// <param name="project"></param>
       public void Save(Project project)
       {
           
           ITransaction transaction = null;
     
           ISession tsession = Session;

           transaction = tsession.BeginTransaction();
           try
           {
              
               tsession.SaveOrUpdate(project);
               transaction.Commit();
             

           }
           catch (Exception)
           {
               //TODO: implement logging
               
           }
           finally
           {
               tsession.Close();
           }


       }

       /// <summary>
       /// Deletes the project object from the database.
       /// </summary>
       /// <param name="project"></param>
       public  void Delete (Project project)
       {
           ITransaction transaction = null;

           ISession tsession = Session;
           transaction = tsession.BeginTransaction();

           try
           {
               tsession.Delete(project);
               transaction.Commit();
           }
           catch (Exception)
           {
                
              
           }
           finally
           {
               tsession.Close();
           }

       }

       /// <summary>
       /// Returns a single project object
       /// </summary>
       /// <param name="projectId"></param>
       /// <returns></returns>
       public  Project GetById (int projectId)
       {
           ISession tsession = Session;

           Project project = tsession.Get<Project>(projectId);

           tsession.Close();

           return project;
       }
       
       /// <summary>
       /// Returns a list of all projects
       /// </summary>
       /// <returns></returns>
       public IList<Project> GetAllProjects()
       {
           ISession tsession = Session;

           ICriteria criteria = tsession.CreateCriteria<Project>();
           IList<Project> projects = criteria.List<Project>();

           tsession.Close();


           return projects;
       }
   }
}
